package com.qf.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 王博康
 * @version 1.0
 * @project 05_springMVC
 * @description: TODO
 * @date 2022/3/8 11:16 星期二
 */
@Controller
@SessionAttributes({"name","age"})//会将key为这个两个名字的值自动存入session中
public class TestController {
    @RequestMapping("/testrequest")
    public  String test1(String name, HttpServletRequest request){
        request.setAttribute("diyige",name );
        return "/laowang.jsp";
    }

    @RequestMapping("/testmodel")
    public  String test2(String name, Model model){
        model.addAttribute("diyige",name );//数据也会放一份到request中
        return "/laowang.jsp";
    }

    @RequestMapping("/testmodelandview")
    public  ModelAndView test3(String name, ModelAndView modelAndView){
        modelAndView.addObject("diyige",name );//添加数据，放入request中
        modelAndView.setViewName("/laowang.jsp");
        return modelAndView;
    }

    @RequestMapping("/testsessionattr")
    public  ModelAndView test4(String name, ModelAndView modelAndView){
        modelAndView.addObject("diyige",name );//添加数据，放入request中
        modelAndView.addObject("name",name );//添加数据，由于类上有@SessionAttributes({"name","age"})注解，该数据会存入session中
        modelAndView.addObject("age",16 );//添加数据，由于类上有@SessionAttributes({"name","age"})注解，该数据会存入session中
        modelAndView.addObject("address","北京" );//添加数据，放入request中
        modelAndView.setViewName("/xiaohong.jsp");//设置转发位置
        return modelAndView;
    }

}
